package com.colorlight.cloud.gateway.api.message;

import com.colorlight.cloud.gateway.api.message.api.XmDefaultProcess;
import com.colorlight.cloud.gateway.api.message.model.Msg;
import com.colorlight.cloud.gateway.lock.LockObject;
import com.colorlight.cloud.xm.entity.enumerate.NameStruct;
import com.colorlight.cloud.xm.entity.event.*;
import com.colorlight.cloud.xm.entity.instructions.*;
import com.colorlight.iot.proto.xm.event.XmEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import static com.colorlight.cloud.common.constants.CommonConstants.GSON;

/**
 * @author Demon
 */
@Slf4j
@Component
public class XmAllEventHandler {
	@Component
	public static class ParamSetInstructionHandler implements XmDefaultProcess<ParamSet> {
		@Override
		public NameStruct getInstructionType() {
			return NameStruct.ParamSetInstruction;
		}
	}
	
	@Component
	public static class SetTimeZoneInstructionHandler implements XmDefaultProcess<SetTimeZone> {
		@Override
		public NameStruct getInstructionType() {
			return NameStruct.SetTimeZoneInstruction;
		}
	}
	
	@Component
	public static class RemoveDeviceInstructionHandler implements XmDefaultProcess<RemoveDevice> {
		@Override
		public NameStruct getInstructionType() {
			return NameStruct.RemoveDeviceInstruction;
		}
	}
	
	@Component
	public static class EncryptInstructionHandler implements XmDefaultProcess<Encrypt> {
		@Override
		public void accept(String deviceName, Msg<Encrypt> msg) {

			log.info("设备DeviceName:{}\n 屏体Uid:{}\n 消息类型：{} \n 消息：{} ", deviceName, msg.getUuid(),msg.getBizContent().getClass().getName(), GSON.toJson(msg));
			synchronized (LockObject.LOCK){
				LockObject.LOCK.notify();
				//线程唤醒
				log.info("Encrypt 唤醒");
			}
		}
		
		@Override
		public NameStruct getInstructionType() {
			return NameStruct.EncryptInstruction;
		}
	}
	
	@Component
	public static class DecryptInstructionHandler implements XmDefaultProcess<Decrypt> {
		
		@Override
		public void accept(String deviceName, Msg<Decrypt> msg) {

			log.info("设备DeviceName:{}\n 屏体Uid:{}\n 消息类型：{} \n 消息：{} ", deviceName, msg.getUuid(),msg.getBizContent().getClass().getName(), GSON.toJson(msg));
			synchronized (LockObject.LOCK){
				LockObject.LOCK.notify();
				//线程唤醒
				log.info("Decrypt 唤醒");
			}
		}
		
		@Override
		public NameStruct getInstructionType() {
			return NameStruct.DecryptInstruction;
		}
	}
	
	@Component
	public static class GetDeviceAuthInfoInstructionHandler implements XmDefaultProcess<GetDeviceAuthInfo> {
		@Override
		public NameStruct getInstructionType() {
			return NameStruct.GetDeviceAuthInfoInstruction;
		}
	}
	
	@Component
	public static class TestNetworkLatencyInstructionHandler implements XmDefaultProcess<TestNetworkLatency> {
		@Override
		public NameStruct getInstructionType() {
			return NameStruct.TestNetworkLatencyInstruction;
		}
	}
	
	@Component
	public static class HBEventHandler implements XmDefaultProcess<HB> {
		@Override
		public NameStruct getInstructionType() {
			return NameStruct.HBEvent;
		}
	}
	
	@Component
	public static class SenderMonitorReportEventHandler implements XmDefaultProcess<SenderMonitorReport> {
		@Override
		public NameStruct getInstructionType() {
			return NameStruct.SenderMonitorReportEvent;
		}
	}
	
	@Component
	public static class OffLineEventHandler implements XmDefaultProcess<OffLine> {
		@Override
		public NameStruct getInstructionType() {
			return NameStruct.OffLineEvent;
		}
	}
}